<template>
  <div v-loading="loading">
    <AplidForm ref="formRef" v-bind="formConfig" v-model="formModel" @submit="onSubmit" @reset="onReset">
      <template v-slot:tableRelForm>
        <remoteSelect v-if="formModel.subsidyType" class="w-100" ref="select1" :remote="false" :filterable="false"
          :key="formModel.subsidyType + '1'" :multiple="true" :dicData="subsidyTableDictData"
          v-model="formModel.tableRel" v-model:modelName="formModel.tableRelLabel" propName="tableRelLabel">
        </remoteSelect>
      </template>
      <template v-slot:policyRelForm>
        <remoteSelect ref="select2" class="w-100" :key="formModel.subsidyType + '2'"
          :paramsObj="{ module: 2, subsidyType: formModel.subsidyType }" api="view_dpPolicyDocument_page" filterKey="id"
          filterLabel="policyTitle" :multiple="true" v-model="formModel.policyRel"
          v-model:modelName="formModel.policyRelLabel" propName="policyRelLabel"></remoteSelect>
      </template>
    </AplidForm>
  </div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { formConfig, formateCallBackValue } from '../config'
import remoteSelect from '@/components/AplidCrud/src/AplidRemote/index'
import { tableConfigOption } from '@/views/instOperate/components/ApplyReport/components/config'
import { getStore } from '@/utils/store';

type Props = {
  nowRowData?: any
  subsidyApplyConfigId?: string | number
}
let props = withDefaults(defineProps<Props>(), {
  type: 'add',
  nowRowData: {},
})
const emit = defineEmits(['refresh'])

const formModel: any = ref({})
const userInfo = getStore('userInfo') || {}
let subsidyTableDictData = computed(() => {
  return tableConfigOption(formModel.value.subsidyType)
})

const loading = ref(false)

const formRef = ref()

const getDetail = () => {
  loading.value = true
  apiMod.view_ofsSubsidyApplyConfig_view(props.subsidyApplyConfigId).then((res) => {
    const item = res.data || {}
    loading.value = false
    formModel.value = {
      ...item,
      ...formateCallBackValue(item, 'tableRel'),
      ...formateCallBackValue(item, 'policyRel'),
      ...formateCallBackValue(item, 'materialRel'),
      ...formateCallBackValue(item, 'dataRel'),
    }
  })
}

const onSubmit = (form, done) => {
  const getStrList = (strValue, strLabel) => {
    if (strValue) {
      const values = strValue.split(',')
      const labels = strLabel.split(',')
      const tempList = labels.map((name, index) => {
        return { label: name, value: values[index] }
      })
      return JSON.stringify(tempList)
    }
  }
  const query = {
    ...form,
    policyRel: getStrList(form.policyRel, form.policyRelLabel),
    tableRel: getStrList(form.tableRel, form.tableRelLabel),
    materialRel: getStrList(form.materialRel, form.materialRelLabel),
    dataRel: getStrList(form.dataRel, form.dataRelLabel),
  }
  let baseApi = !props.subsidyApplyConfigId ? 'view_ofsSubsidyApplyConfig_add' : 'view_ofsSubsidyApplyConfig_edit'
  apiMod[baseApi](query).then(() => {
    ElMessage.success('操作成功')
    done()
    emit('refresh')
  })
}

const onSave = () => {
  formRef.value!.onSubmit()
}

const onReset = () => {
  emit('refresh')
}
onMounted(() => {
  if (props.subsidyApplyConfigId) {
    getDetail()
  } else {
    formModel.value = {
      ...props.nowRowData,
      ...formateCallBackValue(props.nowRowData, 'tableRel'),
      ...formateCallBackValue(props.nowRowData, 'policyRel'),
      ...formateCallBackValue(props.nowRowData, 'materialRel'),
      ...formateCallBackValue(props.nowRowData, 'dataRel'),
    }
  }
})

defineExpose({
  onSave,
})
</script>

<style scoped></style>
